﻿namespace Autorent.Desktop.Modules.Addresses.Services
{
    using System;
    using System.Collections.ObjectModel;
    using System.Linq;
    using Autorent.Desktop.Model;
    using Autorent.Desktop.Model.AutorentMngrServiceReference;


    public class CitiesRepository : AutorentEntitiesContext, ICitiesRepository
    {       
        public ObservableCollection<Country> GetCities()
        {
            try
            {
                return new ObservableCollection<Country>(Context.Countries);
            }
            catch (Exception)
            {
                throw new AutorentModelException("Cities loading error occured!");
            }
        }

        public void AddCity(City city)
        {
            try
            {
                Context.AddToCities(city);
                Context.SaveChanges();
            }
            catch (Exception)
            {
                throw new AutorentModelException("Add city error occured!");
            }
        }

        public void UpdateCity(City city)
        {
            Context.UpdateObject(city);
            Context.SaveChanges();
        }

        public void DeleteCity(City city)
        {
            try
            {
                Context.DeleteObject(Context.Cities.Where(c => c.City_ID.Equals(city.City_ID)));
                Context.SaveChanges();
            }
            catch (Exception)
            {
                throw new AutorentModelException("Delete city error occured!");
            }
        }
    }
}
